<template>
  <el-row :gutter="40" class="panel-group">
    <!--上链企业  -->
    <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
        <div class="card-panel-icon-wrapper icon-people">
          <!-- 图标！！！ -->
          <svg-icon icon-class="tree" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">本年销售量</div>
          <count-to :start-val="0" :end-val="saleWeight" :duration="2800" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <!-- 区块交易 -->
    <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('purchases')">
        <div class="card-panel-icon-wrapper icon-tree">
          <svg-icon icon-class="node" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">区块高度</div>
          <count-to :start-val="0" :end-val="blockHeight" :duration="4000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <!-- 智能合约 -->
    <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="contract" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">智能合约数</div>
          <count-to :start-val="0" :end-val="10" :duration="2200" class="card-panel-num" />
        </div>
      </div>
    </el-col>

    <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-search">
          <svg-icon icon-class="blockchain1" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">区块交易数量</div>
          <count-to :start-val="0" :end-val="block" :duration="4000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-equipment">
          <svg-icon icon-class="equipment" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">训练模型数</div>
          <count-to :start-val="0" :end-val="8" :duration="2000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-edit">
          <svg-icon icon-class="edit" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">系统用户数</div>
          <count-to :start-val="0" :end-val="total" :duration="1600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from "vue-count-to";

export default {
  components: { CountTo },
  data() {
    return {
      loading: true,
      saleWeight: 0,
      blockHeight: 0,
      block: 0,
      total: 0,
      refreshInterval: null
    };
  },
  created() {
    this.loadData();
  },
  beforeDestroy() {
    // 清除定时器
    if (this.refreshInterval) {
      clearInterval(this.refreshInterval);
    }
  },
  methods: {
    async loadData() {
      this.loading = true;
      try {
        // 模拟API请求
        await new Promise(resolve => setTimeout(resolve, 300));
        
        // 这里替换为实际API调用
        const mockData = {
          user: { total: 11 },
          sale: { saleWeight: 5275 },
          blockTrans: { items: [{ block: 10705 }] },
          blockHeight: { items: [{ height: 26690 }] }
        };
        
        this.total = mockData.user.total;
        this.saleWeight = mockData.sale.saleWeight;
        this.block = mockData.blockTrans.items[0].block;
        this.blockHeight = mockData.blockHeight.items[0].height;
        
        // 如果需要实时更新，可以保留定时刷新，但建议增加节流
        // this.setupAutoRefresh();
      } catch (error) {
        console.error("加载数据失败:", error);
      } finally {
        this.loading = false;
      }
    },
    
    // 可选：如果需要实时数据
    setupAutoRefresh() {
      // 清除已有定时器
      if (this.refreshInterval) {
        clearInterval(this.refreshInterval);
      }
      
      // 设置较长的刷新间隔（如30秒或1分钟）
      this.refreshInterval = setInterval(() => {
        this.loadData();
      }, 30000); // 30秒刷新一次
    },
    
    handleSetLineChartData(type) {
      this.$emit("handleSetLineChartData", type);
    }
  }
};
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-equipment {
        background:#4ab3799a;
      }

      .icon-tree {
        background: #3f7121;
      }

      .icon-edit {
        background: #34bfa3;
      }

      .icon-search {
        background: #c4b7e0;
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-bug {
      color: #f4516c;
    }

    .icon-tree {
      color: #3f7121;
    }

    .icon-edit {
      color: #34bfa3;
    }

    .icon-search {
      color: #c4b7e0;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        text-align: center;
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
